<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class CateController extends CommonController {
    public function lst(){
		
		$mod=D('cate');
		$list = $mod->catetree();			//调用catetree()进行无极排序，
		$this->assign('list',$list);
		$this->display();
	}
	
	public function add(){
		$mod=D('cate');
		if(IS_POST){
				if($mod->create()){
					if($mod->add()){
						$this->success("添加栏目成功！",U("lst"));
					}else{
						$this->error("添加栏目失败！");
					}
				}else{
					$this->error($mod->getError());
				}	
				return;
		}
		
		$lst = $mod->catetree();			//调用catetree()进行无极排序，
		$this->assign('lst',$lst);
		$recposres=D('recpos')->where(array('rectype'=>2))->select();
		$this->assign('recposres',$recposres);
		$this->display();
	}
	
	
	public function edit(){
		$id=I('id');
		$mod=D('cate');
		if(IS_POST){
			if($mod->create()){
				if($mod->save()!==false){
					$this->success("修改栏目信息成功！",U('lst'));
				}else{
					$this->error("修改栏目信息失败！");
				}
				
			}else{
				$mod->error($mod->getError());
			}
			return;
		}
		$res = $mod->find(I('id'));//查询从lst页面传递过来id号的数据；
		$list = $mod->catetree();			//调用catetree()进行无极排序，
		$recpos=D('recpos')->where(array('rectype'=>2))->select();//查询当前商品的推荐位属性
		$_recvalues=D('recvalue')->field('recid')->where(array('rectype'=>2,'valueid'=>$id))->select();
		//将查询到的商品推荐位进行遍历，并存放到一维数组中
		$recids=array();
		foreach($_recvalues as $k=>$v){
			$recids[]=$v['recid'];
		}
		$this->assign(array(
				'res'=>$res,
				'list'=>$list,
				'recids'=>$recids,
				'recpos'=>$recpos,
				
		));
		$this->display();
	}
	
	public function del($id){
		$mod = D('cate');
		$goods=D('goods');
		$recvalue=D('recvalue');
		$res = $mod->catedel($id);		//使用递归的函数得到要删除的这条纪录的id及其下面的了栏目的id;
		 $de = implode(",", $res);
		 $goodsres=$goods->where("cate_id IN($de)")->select();
		 if($goodsres){
			 foreach($goodsres as $k=>$v){
				 @unlink($v['original']);
				 @unlink($v['sm_thumb']);
				 @unlink($v['mid_thumb']);
				 @unlink($v['max_thumb']);
				 $goods->delete($v['id']);
				 $recvalue->where(array('valueid'=>$v['id'],'rectype'=>1))->delete();
			 }
		 }
		 
		if($mod->delete($de)){
			$this->success("删除纪录成功！",U("lst"));
		}else{
			$this->error("删除纪录失败！");
		}
		
	}
	
}